package com.example.designer.observer.impl;

import com.example.designer.observer.inter.Observer;
import com.example.designer.observer.inter.Subject;

import java.util.ArrayList;

/**
 * 主题实现
 */
public class WeatherData implements Subject {

    private ArrayList<Observer> observerList;//存储所有观察者
    //下面是主题状态  当状态修改时  去通知观察者
    private float temperature;//温度
    private float humidity;//湿度
    private float pressure;//压力

    public WeatherData() {
        this.observerList = new ArrayList<Observer>();
    }

    //注册
    @Override
    public void registerObserver(Observer o) {
        observerList.add(o);
    }

    //取消注册
    @Override
    public void remoteObserver(Observer o) {
        observerList.remove(o);
    }

    //通知
    @Override
    public void notifyObserver() {
        for (Observer o : observerList) {
            o.update(temperature,humidity,pressure);
        }
    }

    //当主题数据变化时 通知观察者
    public void measurementChange(){
        notifyObserver();
    }

    //模拟修改主题数据
    public void setMeasureMent(float temperature, float humidity, float pressure) {
        this.temperature = temperature;
        this.humidity = humidity;
        this.pressure = pressure;
        measurementChange();
    }
}
